<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>默认实参</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/default_arguments","wgTitle":"cpp/language/default arguments","wgCurRevisionId":65112,"wgArticleId":9182,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Pages with unreviewed CWG DR marker"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/default_arguments","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_default_arguments skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">默认实参</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="https://zh.cppreference.com/w/cpp/container" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="functions.html" title="cpp/language/functions"> 函数</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> 声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> 函数声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html#.E5.BD.A2.E5.8F.82.E5.88.97.E8.A1.A8" title="cpp/language/function"> 函数形参列表</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html#.E5.87.BD.E6.95.B0.E5.AE.9A.E4.B9.89" title="cpp/language/function"> 函数定义</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> 默认实参</strong> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="variadic_arguments.html" title="cpp/language/variadic arguments"> 变长实参</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="inline.html" title="cpp/language/inline"> inline 说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lambda.html" title="cpp/language/lambda"> lambda 表达式</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="coroutines.html" title="cpp/language/coroutines"> 协程</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 函数调用 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="adl.html" title="cpp/language/adl"> 实参依赖查找 (ADL)</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_other.html#.E5.86.85.E5.BB.BA.E7.9A.84.E5.87.BD.E6.95.B0.E8.B0.83.E7.94.A8.E8.BF.90.E7.AE.97.E7.AC.A6" title="cpp/language/operator other"> 函数调用运算符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req/FunctionObject.html" title="cpp/named req/FunctionObject"> 函数对象</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 重载 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="overload_resolution.html" title="cpp/language/overload resolution"> 重载决议</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operators.html" title="cpp/language/operators"> 运算符重载</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="overloaded_address.html" title="cpp/language/overloaded address"> 重载集地址</a> </td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>允许调用函数时不提供一个或多个尾部的实参。
</p><p>通过为<a href="function.html" title="cpp/language/function">函数声明</a>的 <span class="t-spar">形参列表</span> 中的形参使用下列语法来指定默认实参。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">attr</span><span class="t-mark">(可选)</span> <span class="t-spar">声明说明符序列</span> <span class="t-spar">声明符</span> <code><b>= </b></code> <span class="t-spar">初始化器</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">attr</span><span class="t-mark">(可选)</span> <span class="t-spar">声明说明符序列</span> <span class="t-spar">抽象声明符</span><span class="t-mark">(可选)</span> <code><b>= </b></code> <span class="t-spar">初始化器</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>默认实参用于取代函数调用中缺失的尾部实参：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> point<span class="br0">(</span><span class="kw4">int</span> x <span class="sy1">=</span> <span class="nu0">3</span>, <span class="kw4">int</span> y <span class="sy1">=</span> <span class="nu0">4</span><span class="br0">)</span><span class="sy4">;</span>
 
point<span class="br0">(</span><span class="nu0">1</span>,<span class="nu0">2</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 调用 point(1,2)</span>
point<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span>   <span class="co1">// 调用 point(1,4)</span>
point<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>    <span class="co1">// 调用 point(3,4)</span></pre></div></div>
<p>在函数声明中，在拥有默认实参的形参之后，所有后续形参必须
</p>
<ul><li> 拥有在这个或同一作用域中先前的声明中所提供的默认实参；或
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> x<span class="br0">(</span><span class="kw4">int</span> <span class="sy1">=</span> <span class="nu0">1</span>, <span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误，假定没有先前的 x 的声明</span>
 
<span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span> n, <span class="kw4">int</span> k <span class="sy1">=</span> <span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">0</span>, <span class="kw4">int</span> k<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK：同一作用域中之前的声明为 k 提供了默认实参</span>
 
<span class="kw4">void</span> g<span class="br0">(</span><span class="kw4">int</span>, <span class="kw4">int</span> <span class="sy1">=</span> <span class="nu0">7</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">void</span> h<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
  <span class="kw4">void</span> g<span class="br0">(</span><span class="kw4">int</span> <span class="sy1">=</span> <span class="nu0">1</span>, <span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：不在同一作用域</span>
<span class="br0">}</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<dl><dd><ul><li> ...除非该形参是从某个形参包展开得到的
</li></ul>
</dd></dl>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">class</span> ...<span class="me1">T</span><span class="sy1">&gt;</span> <span class="kw1">struct</span> C <span class="br0">{</span> <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">0</span>, T...<span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
C<span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> c<span class="sy4">;</span>  <span class="co1">// OK；实例化了声明 void C::f(int n = 0, int)</span></pre></div></div>
<ul><li> 或者是函数形参包。
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">class</span>...<span class="me1">T</span><span class="sy1">&gt;</span> <span class="kw4">void</span> h<span class="br0">(</span><span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">0</span>, T... <span class="me1">args</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// ok</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<p>省略号不是形参，故可以跟在带有默认实参的形参之后。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">0</span>, ...<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// ok</span></pre></div></div>
<p>默认实参仅允许于<a href="function.html" title="cpp/language/function">函数声明</a><span class="t-rev-inl t-since-cxx14"><span>和 <a href="lambda.html" title="cpp/language/lambda">lambda 表达式</a></span> <span><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></span></span>的形参列表中出现，而不允许在函数指针、到函数的引用，或在 <a href="typedef.html" title="cpp/language/typedef">typedef</a> 声明中出现。模板形参列表为其<a href="template_parameters.html#.E9.BB.98.E8.AE.A4.E6.A8.A1.E6.9D.BF.E5.AE.9E.E5.8F.82" title="cpp/language/template parameters">默认模板实参</a>使用了类似的语法。
</p><p>对于非模板函数，当在同一作用域中重声明函数时，可以向已声明的函数添加默认实参。在函数调用点，可用的默认实参是由该函数所有可见的声明中所提供的默认实参的并集。重声明不能为已有可见默认值的实参引入默认值（即使值相同）。内层作用域中的重声明不从外层作用域获得默认实参。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span>, <span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>     <span class="co1">// #1 </span>
<span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span>, <span class="kw4">int</span> <span class="sy1">=</span> <span class="nu0">7</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// #2 OK ：添加到默认</span>
 
<span class="kw4">void</span> h<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    f<span class="br0">(</span><span class="nu0">3</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// #1 与 #2 在作用域中；进行对 f(3,7) 的调用</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span> <span class="sy1">=</span><span class="nu0">1</span>, <span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：内层作用域声明不获得默认实参</span>
<span class="br0">}</span>
 
<span class="kw4">void</span> m<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="co1">// 新作用域开始</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span>, <span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 内层作用域声明；无默认实参。</span>
    f<span class="br0">(</span><span class="nu0">4</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：调用 f(int, int) 的实参不足</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span>, <span class="kw4">int</span> <span class="sy1">=</span> <span class="nu0">6</span><span class="br0">)</span><span class="sy4">;</span> 
    f<span class="br0">(</span><span class="nu0">4</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK：调用 f(4,6);</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span>, <span class="kw4">int</span> <span class="sy1">=</span> <span class="nu0">6</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：不能在同一作用域中重声明默认实参</span>
<span class="br0">}</span>
 
<span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span> <span class="sy1">=</span> <span class="nu0">1</span>, <span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// #3 OK，向 #2 添加默认实参</span>
 
<span class="kw4">void</span> n<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="co1">// 新作用域开始</span>
    f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// #1、#2 及 #3 在作用域中：调用 f(1, 7);</span>
<span class="br0">}</span></pre></div></div>
<p>若某个 <a href="inline.html" title="cpp/language/inline">inline</a> 函数声明于不同翻译单元中，则默认实参的积累集必须在每个翻译单元的结尾相同。
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<p>若在不同翻译单元的同一命名空间作用域中声明一个非 inline 函数，则对应的默认实参若存在则必须相同（但一些某些翻译单元中可以缺少一些默认实参）。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<p>若 <a href="friend.html" title="cpp/language/friend">friend</a> 声明指定了默认实参，则它必须是友元函数定义，且该翻译单元中不允许此函数的其他声明。
</p><p><a href="namespace.html#using_.E5.A3.B0.E6.98.8E" title="cpp/language/namespace">using 声明</a>将已知的默认实参集承接过来，且若向函数的命名空间中添加更多默认形参，则这些默认实参在这条 using 声明可见的任何位置均亦为可见
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> N <span class="br0">{</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span>, <span class="kw4">int</span> <span class="sy1">=</span> <span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw1">using</span> N<span class="sy4">::</span><span class="me2">f</span><span class="sy4">;</span>
<span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    f<span class="br0">(</span><span class="nu0">7</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 调用 f(7, 1);</span>
    f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>  <span class="co1">// 错误</span>
<span class="br0">}</span>
<span class="kw1">namespace</span> N <span class="br0">{</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span> <span class="sy1">=</span> <span class="nu0">2</span>, <span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw4">void</span> h<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>  <span class="co1">// 调用 f(2, 1);</span>
<span class="br0">}</span></pre></div></div>
<p>对默认实参中使用的名字进行查找，检查<a href="access.html" title="cpp/language/access">可访问性</a>，并绑定于声明点，但在函数调用点才执行：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> a <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">int</span> x <span class="sy1">=</span> f<span class="br0">(</span>a<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// f 的查找找到 ::f，a 的查找找到 ::a</span>
                     <span class="co1">// 不使用 ::a 的值，它在此点为 1</span>
<span class="kw4">void</span> h<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
  a <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>  <span class="co1">// 更改 ::a 的值</span>
  <span class="br0">{</span>
     <span class="kw4">int</span> a <span class="sy1">=</span> <span class="nu0">3</span><span class="sy4">;</span>
     g<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>       <span class="co1">// 调用 f(2)，然后以结果调用 g()</span>
  <span class="br0">}</span>
<span class="br0">}</span></pre></div></div>
<p>对于非模板类的<a href="member_functions.html" title="cpp/language/member functions">成员函数</a>，类外的定义中允许出现默认实参，并与类体内的声明所提供的默认实参组合。若类外的默认实参会使成员函数变成默认、复制或移动构造函数，则程序非良构。对于类模板的成员函数，所有默认实参必须在成员函数的初始声明处提供。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">class</span> C <span class="br0">{</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">3</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">void</span> g<span class="br0">(</span><span class="kw4">int</span> i, <span class="kw4">int</span> j <span class="sy1">=</span> <span class="nu0">99</span><span class="br0">)</span><span class="sy4">;</span>
    C<span class="br0">(</span><span class="kw4">int</span> arg<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 非默认构造函数</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">void</span> C<span class="sy4">::</span><span class="me2">f</span><span class="br0">(</span><span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">3</span><span class="br0">)</span> <span class="br0">{</span>         <span class="co1">// 错误：默认实参已指定于类作用域</span>
<span class="br0">}</span>
<span class="kw4">void</span> C<span class="sy4">::</span><span class="me2">g</span><span class="br0">(</span><span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">88</span>, <span class="kw4">int</span> j<span class="br0">)</span> <span class="br0">{</span> <span class="co1">// OK：此翻译单元中，可无实参调用 C::g</span>
<span class="br0">}</span>
C<span class="sy4">::</span><span class="me2">C</span><span class="br0">(</span><span class="kw4">int</span> arg <span class="sy1">=</span> <span class="nu0">1</span><span class="br0">)</span> <span class="br0">{</span>   <span class="co1">// 错误：使之变成了默认构造函数</span>
<span class="br0">}</span></pre></div></div>
<p><a href="virtual.html" title="cpp/language/virtual">虚</a>函数的覆盖函数不会从基类定义获得默认实参，而在进行虚函数调用时，默认实参根据对象的静态类型确定（注意：这可以通过<a rel="nofollow" class="external text" href="http://www.gotw.ca/publications/mill18.htm">非虚接口</a>模式避免）。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> Base <span class="br0">{</span>
    <span class="kw1">virtual</span> <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span> a <span class="sy1">=</span> <span class="nu0">7</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">struct</span> Derived <span class="sy4">:</span> Base <span class="br0">{</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span> a<span class="br0">)</span> override<span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">void</span> m<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    Derived d<span class="sy4">;</span>
    Base<span class="sy3">&amp;</span> b <span class="sy1">=</span> d<span class="sy4">;</span>
    b.<span class="me1">f</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK：调用 Derived::f(7) </span>
    d.<span class="me1">f</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：无默认实参</span>
<span class="br0">}</span></pre></div></div>
<p>默认实参中不允许使用局部变量<span class="t-rev-inl t-since-cxx14"><span>，除非用于不求值语境中</span> <span><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></span></span>：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span> 
<span class="br0">{</span>
    <span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
    <span class="kw4">extern</span> <span class="kw4">void</span> g<span class="br0">(</span><span class="kw4">int</span> x <span class="sy1">=</span> n<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：局部变量不能为默认</span>
    <span class="kw4">extern</span> <span class="kw4">void</span> h<span class="br0">(</span><span class="kw4">int</span> x <span class="sy1">=</span> sizeof n<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// CWG 2082 起 OK</span>
<span class="br0">}</span></pre></div></div>
<p>默认实参中不允许 <a href="this.html" title="cpp/language/this">this</a> 指针：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">class</span> A <span class="br0">{</span>
  <span class="kw4">void</span> f<span class="br0">(</span>A<span class="sy2">*</span> p <span class="sy1">=</span> this<span class="br0">)</span> <span class="br0">{</span> <span class="br0">}</span> <span class="co1">// 错误：不允许 this</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<p>默认实参中不允许使用非静态的类成员（即使它们不被求值），除非用于构成成员指针或在成员访问表达式中使用。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> b<span class="sy4">;</span>
<span class="kw1">class</span> X <span class="br0">{</span>
  <span class="kw4">int</span> a<span class="sy4">;</span>
  <span class="kw4">int</span> mem1<span class="br0">(</span><span class="kw4">int</span> i <span class="sy1">=</span> a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：不能使用非静态数据成员</span>
  <span class="kw4">int</span> mem2<span class="br0">(</span><span class="kw4">int</span> i <span class="sy1">=</span> b<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK：查找找到静态成员 X::b</span>
  <span class="kw4">static</span> <span class="kw4">int</span> b<span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<p>默认实参中不允许使用函数形参<span class="t-rev-inl t-until-cxx14"><span>（即使它们不被求值）</span> <span><span class="t-mark-rev t-until-cxx14">(C++14 前)</span></span></span><span class="t-rev-inl t-since-cxx14"><span>除非它们不被求值</span> <span><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></span></span>。注意，形参列表中较早出现的形参已在<a href="scope.html" title="cpp/language/scope">作用域</a>中：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> a<span class="sy4">;</span>
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span> a, <span class="kw4">int</span> b <span class="sy1">=</span> a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：形参用作默认实参</span>
<span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">int</span> a, <span class="kw4">int</span> b <span class="sy1">=</span> sizeof a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// CWG 2082 为止错误</span>
                                <span class="co1">// CWG 2082 后 OK：用于不求值语境是 OK 的</span></pre></div></div>
<p>默认实参不是函数类型的一部分
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span> <span class="sy1">=</span> <span class="nu0">0</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">void</span> h<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
  <span class="kw4">int</span> j <span class="sy1">=</span> f<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span>
  <span class="kw4">int</span> k <span class="sy1">=</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>  <span class="co1">// 调用 f(0);</span>
<span class="br0">}</span>
<span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span>p1<span class="br0">)</span><span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span> <span class="sy1">=</span> <span class="sy3">&amp;</span>f<span class="sy4">;</span>
<span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span>p2<span class="br0">)</span><span class="br0">(</span><span class="br0">)</span>    <span class="sy1">=</span> <span class="sy3">&amp;</span>f<span class="sy4">;</span> <span class="co1">// 错误：f 的类型是 int(int)</span></pre></div></div>
<p><br>
若 <a href="lambda.html" title="cpp/language/lambda">lambda 表达式</a>出现于默认实参中，则它不能显式或隐式俘获任何内容。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> f2<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
    <span class="kw4">void</span> g1<span class="br0">(</span><span class="kw4">int</span> <span class="sy1">=</span> <span class="br0">(</span><span class="br0">[</span>i<span class="br0">]</span><span class="br0">{</span> <span class="kw1">return</span> i<span class="sy4">;</span> <span class="br0">}</span><span class="br0">)</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：俘获某些内容</span>
    <span class="kw4">void</span> g2<span class="br0">(</span><span class="kw4">int</span> <span class="sy1">=</span> <span class="br0">(</span><span class="br0">[</span>i<span class="br0">]</span><span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">0</span><span class="sy4">;</span> <span class="br0">}</span><span class="br0">)</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：俘获某些内容</span>
    <span class="kw4">void</span> g3<span class="br0">(</span><span class="kw4">int</span> <span class="sy1">=</span> <span class="br0">(</span><span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span><span class="br0">{</span> <span class="kw1">return</span> i<span class="sy4">;</span> <span class="br0">}</span><span class="br0">)</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：俘获某些内容</span>
    <span class="kw4">void</span> g4<span class="br0">(</span><span class="kw4">int</span> <span class="sy1">=</span> <span class="br0">(</span><span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span><span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">0</span><span class="sy4">;</span> <span class="br0">}</span><span class="br0">)</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>       <span class="co1">// OK：无俘获</span>
    <span class="kw4">void</span> g5<span class="br0">(</span><span class="kw4">int</span> <span class="sy1">=</span> <span class="br0">(</span><span class="br0">[</span><span class="br0">]</span><span class="br0">{</span> <span class="kw1">return</span> sizeof i<span class="sy4">;</span> <span class="br0">}</span><span class="br0">)</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK：无俘获</span>
<span class="br0">}</span></pre></div></div>
<p><br>
除了函数调用运算符外，运算符函数不应有默认参数。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">class</span> C <span class="br0">{</span>
    <span class="kw4">int</span> operator<span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">0</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 非良构</span>
    <span class="kw4">int</span> operator<span class="br0">(</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">int</span> x <span class="sy1">=</span> <span class="nu0">0</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// ok</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">缺陷报告</span></h3>
<p>下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> 应用于
</th>
<th> 出版时的行为
</th>
<th> 正确行为
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue2082">CWG 2082</a>
</td>
<td> C++14
</td>
<td> 曾禁止默认实参在不求值语境中使用局部变量
</td>
<td> 允许不求值语境的使用
</td></tr></table>

<!-- 
NewPP limit report
Preprocessor visited node count: 1641/1000000
Preprocessor generated node count: 4916/1000000
Post‐expand include size: 36925/2097152 bytes
Template argument size: 9210/2097152 bytes
Highest expansion depth: 13/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:9182-0!*!0!*!*!*!zh!* and timestamp 20200225050222 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/default_arguments&amp;oldid=65112">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/default_arguments&amp;oldid=65112</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/default_arguments">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2020年2月24日 (星期一) 21:02。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过3,860次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.061 secs. -->
	</body>
<!-- Cached 20200225101553 -->
</html>